home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 118_01 / toolib1.bds < prev    next >
Text File  |  1980-07-08  |  14KB  |  706 lines

  1. /* New BDS library for use with software tools
  2.  * source: toolib1.bds
  3.  * version: November 26, 1981
  4.  */
  5.  
  6. #include tools.h
  7.  
  8. /*
  9.    This file contains all the standard BDS C library functions
  10.    which are written in C that are used by the software tools
  11.    programs.
  12.  
  13.    These library functions are identical to the functions
  14.    which were on files stdlib1 and stdlib2.c
  15.    EXCEPT that these functions call some routines whose names
  16.    have changed.
  17.  
  18.    The object code for these functions used to be on deff.crl.
  19.    Now the object code is on toolib1.crl.
  20.  
  21.    The functions on this file ONLY use functions on toolib2.crl
  22.    All the functions with new names are on file toolib2.crl.
  23.    All functions on toolib2.crl came from deff2.crl.
  24.  
  25.    The names have all been changed in the same way,
  26.    viz., by adding a leading underscore character to the
  27.    name.  The following functions have been renamed:
  28.  
  29.    ( * denotes that a call to the function is in this file)
  30.  
  31. _getchar    _ungetch    _putchar *    _putch
  32. _gets *        _call        _read *        _write
  33. _open *        _close *    _creat        (_unlink)
  34. _seek        (_tell)        _rename        _exit
  35. _rread        _rwrite        _rseek        _rtell
  36. _rsrec        _rcfsiz
  37.  
  38.  
  39.    The following functions were on stdlib1.c but
  40.    have been deleted from this file.
  41.  
  42.    getc(), putc(), and ungetc() are now defined on file.bds.
  43.    They had to be completely rewritten to allow for I/O
  44.    redirection.
  45.  
  46. fopen        getc        ungetc        getw
  47. fcreat        putc        putw
  48. fflush        fclose
  49.  
  50. strcat        strcmp        strcpy        strlen
  51. qsort
  52. initw        initb        getval
  53.  
  54.    The following functions from stdlib1.c are on this file:
  55.  
  56. qsort
  57. atoi
  58. isalpha        isupper        islower        isdigit
  59. isspace        toupper        tolower
  60. alloc        free
  61. abs        max        min
  62.  
  63. */
  64.  
  65. /*
  66. This is the new qsort routine, utilizing the shell sort
  67. technique given in the Software Tools book (by Kernighan 
  68. & Plauger.)
  69.  
  70. NOTE: this "qsort" function is different from the "qsort" given
  71. in some old releases (pre 1.32) -- here, the items are sorted
  72. in ASCENDING order.
  73. */
  74.  
  75. qsort(base, nel, width, compar)
  76. char *base; int (*compar)();
  77. {    int gap,ngap, i, j;
  78.     int jd, t1, t2;
  79.     t1 = nel * width;
  80.     for (ngap = nel / 2; ngap > 0; ngap /= 2) {
  81.        gap = ngap * width;
  82.        t2 = gap + width;
  83.        jd = base + gap;
  84.        for (i = t2; i <= t1; i += width)
  85.           for (j =  i - t2; j >= 0; j -= gap) {
  86.         if ((*compar)(base+j, jd+j) <=0) break;
  87.              _swp(width, base+j, jd+j);
  88.           }
  89.     }
  90. }
  91.  
  92. _swp(w,a,b)
  93. char *a,*b;
  94. int w;
  95. {
  96.     char tmp;
  97.     while(w--) {tmp=*a; *a++=*b; *b++=tmp;}
  98. }
  99.  
  100.  
  101. /*
  102.     Some string functions
  103. */
  104.  
  105. int atoi(n)
  106. char *n;
  107. {
  108.     int val; 
  109.     char c;
  110.     int sign;
  111.     val=0;
  112.     sign=1;
  113.     while ((c = *n) == '\t' || c== ' ') ++n;
  114.     if (c== '-') {sign = -1; n++;}
  115.     while (  isdigit(c = *n++)) val = val * 10 + c - '0';
  116.     return sign*val;
  117. }
  118.  
  119.  
  120. /*
  121.     Some character diddling functions
  122. */
  123.  
  124. int isalpha(c)
  125. char c;
  126. {
  127.     return isupper(c) || islower(c);
  128. }
  129.  
  130.  
  131. int isupper(c)
  132. char c;
  133. {
  134.     return c>='A' && c<='Z';
  135. }
  136.  
  137.  
  138. int islower(c)
  139. char c;
  140. {
  141.     return c>='a' && c<='z';
  142. }
  143.  
  144.  
  145. int isdigit(c)
  146. char c;
  147. {
  148.     return c>='0' && c<='9';
  149. }
  150.  
  151.  
  152. int isspace(c)
  153. char c;
  154. {
  155.     return c==' ' || c=='\t' || c=='\n';
  156. }
  157.  
  158.  
  159. char toupper(c)
  160. char c;
  161. {
  162.     return islower(c) ? c-32 : c;
  163. }
  164.  
  165.  
  166. char tolower(c)
  167. char c;
  168. {
  169.     return isupper(c) ? c+32 : c;
  170. }
  171.  
  172.  
  173. /*
  174. Storage allocation routines, taken from chapter 8 of K&R, but
  175. simplified to ignore the storage allignment problem and not
  176. bother with the "morecore" hack (a call to "sbrk" under CP/M is
  177. a relatively CHEAP operation, and can be done on every call to
  178. "alloc" without degrading efficiency.)
  179. */
  180.  
  181. char *alloc(nbytes)
  182. unsigned nbytes;
  183. {
  184.     struct _header *p, *q, *cp;
  185.     int nunits; 
  186.  
  187.     /* comment out -----
  188.     printf("In alloc: nbytes = %d\n", nbytes);
  189.     ----- end comment out */
  190.  
  191.     nunits = 1 + (nbytes + (sizeof (_base) - 1)) / sizeof (_base);
  192.     if ((q = _allocp) == NULL) {
  193.         _base._ptr = _allocp = q = &_base;
  194.         _base._size = 0;
  195.      }
  196.     for (p = q -> _ptr; ; q = p, p = p -> _ptr) {
  197.         if (p -> _size >= nunits) {
  198.             if (p -> _size == nunits)
  199.                 q -> _ptr = p -> _ptr;
  200.             else {
  201.                 p -> _size -= nunits;
  202.                 p += p -> _size;
  203.                 p -> _size = nunits;
  204.              }
  205.             _allocp = q;
  206.  
  207.             /* comment out -----
  208.             printf("Alloc returns: %x\n", p+1);
  209.             ----- end comment out */
  210.  
  211.             return p + 1;
  212.         }
  213.         if (p == _allocp) {
  214.             cp = sbrk(nunits * sizeof (_base));
  215.             if (cp == ERROR) {
  216.                 printf("Alloc: Out of memory");
  217.                 return NULL;
  218.             }
  219.             cp -> _size = nunits; 
  220.             /* remember:  pointer arithmetic ! */
  221.             free(cp+1);    
  222.             p = _allocp;
  223.         }
  224.     }
  225. }
  226.  
  227.  
  228. free(ap)
  229. struct _header *ap;
  230. {
  231.     struct _header *p, *q;
  232.  
  233.     /* comment out -----
  234.     printf("In free:  ap = %x\n", ap);
  235.     ----- end comment out */
  236.  
  237.     p = ap - 1;    /* No need for the cast when "ap" is a struct ptr */
  238.  
  239.     for (q = _allocp; !(p > q && p < q -> _ptr); q = q -> _ptr)
  240.         if (q >= q -> _ptr && (p > q || p < q -> _ptr))
  241.             break;
  242.     if (p + p -> _size == q -> _ptr) {
  243.         p -> _size += q -> _ptr -> _size;
  244.         p -> _ptr = q -> _ptr -> _ptr;
  245.      }
  246.     else p -> _ptr = q -> _ptr;
  247.  
  248.     if (q + q -> _size == p) {
  249.         q -> _size += p -> _size;
  250.         q -> _ptr = p -> _ptr;
  251.      }
  252.     else q -> _ptr = p;
  253.  
  254.     _allocp = q;
  255.  
  256.     /* comment out -----
  257.     printf("In free:  new _allocp = %x\n", q);
  258.     ----- end comment out */
  259.  
  260. }
  261.  
  262.  
  263. /*
  264.     define max(), min(), abs()
  265. */
  266.  
  267. int abs(n)
  268. {
  269.     return (n<0) ? -n : n;
  270. }
  271.  
  272. int max(a,b)
  273. {
  274.     return (a > b) ? a : b;
  275. }
  276.  
  277. int min(a,b)
  278. {
  279.     return (a <= b) ? a : b;
  280. }
  281.  
  282.  
  283.  
  284. /*
  285.    The following functions from stdlib2.c are on this file:
  286.  
  287.     printf        _spr 
  288.     scanf        _scn
  289.     _puts  (renamed by analogy to _get)
  290.     swapin
  291.  
  292.     The following functions from stdlib2 have been deleted:
  293.  
  294.     fprintf        sprintf
  295.     scanf        sscanf
  296.     fputs        fgets
  297.  
  298.  
  299.     Many comments have been deleted from these functions.
  300.     See STDLIB2.C if you are really interested.
  301.  
  302.     Each function uses an array of dimension MAXLINE.
  303.     Make sure MAXLINE is longer than the largest line
  304.     ever printed!
  305.  
  306.     Remember to put out a 0x1a (control-Z, CPMEOF) at
  307.     the end of text files being written out to disk.
  308.  
  309.     Also remember that there are two end of file
  310.     conditions:  -1 and CPMEOF.
  311. */
  312.  
  313.  
  314. /*
  315.     printf
  316.  
  317.     usage:
  318.         printf(format, arg1, arg2, ...);
  319.     
  320.     Exactly the same as in Kernighan & Ritchie.
  321. */
  322.  
  323. printf(format)
  324. char *format;
  325. {
  326.     char line[MAXLINE];
  327.  
  328.     _spr(line,&format);    /* use "_spr" to form the output */
  329.  
  330.     /* comment out -----
  331.     _puts(line);        /* and print out the line     */
  332.     ----- end comment out */
  333.     putlin(line, STDOUT);
  334. }
  335.  
  336.  
  337. /*
  338.     scanf:
  339.     This one accepts a line of input text from the
  340.     console, and converts the text to the required
  341.     binary or alphanumeric form. (see K & R)
  342.  
  343.     Usage:
  344.         scanf(format, ptr1, ptr2, ...);
  345.  
  346.     Returns number of items matched.
  347.  
  348.     Since a new line of text must be entered from the
  349.     console each time scanf is called, any unprocessed
  350.     text left over from the last call is lost forever.
  351.     This is a difference between BDS  and UNIX.
  352.  
  353.     The field width specification is not supported.
  354. */
  355.  
  356. int scanf(format)
  357. char *format;
  358. {
  359.     char line[MAXLINE];
  360.  
  361.     /* comment out -----
  362.     _gets(line);    /* get a line of input from user */
  363.     ----- end comment out */
  364.     getlin(line, STDIN);
  365.  
  366.     return _scn(line,&format);    /* and scan it with "_scn"     */
  367. }
  368.  
  369.  
  370. /*
  371.     sprintf:
  372.     Like fprintf, except a string pointer is specified
  373.     instead of a buffer pointer. The text is written
  374.     directly into memory where the string pointer points.
  375.  
  376.     Usage:
  377.         sprintf(string,format,arg1, arg2, ...);
  378. */
  379.  
  380. sprintf(buffer,format)
  381. char *buffer, *format;
  382. {
  383.     _spr(buffer,&format);    /* call _spr to do all the work */
  384. }
  385.  
  386.  
  387. /*
  388.     sscanf:
  389.  
  390.     Reads a line of text in from the console and scans it
  391.     for variable values specified in the format string.
  392.     See the comments below in _scn()
  393.  
  394.     Usage:
  395.         sscanf(format,&arg1,&arg2,...);
  396. */
  397.  
  398. int sscanf(line,format)
  399. char *line, *format;
  400. {
  401.     return _scn(line,&format);    /* let _scn do all the work */
  402. }
  403.  
  404.  
  405.  
  406. /*
  407.     General formatted output conversion routine, used by
  408.     fprintf and sprintf..."line" is where the output is
  409.     written, and "fmt" is a pointer to an argument list 
  410.     which must consist of a format string pointer and
  411.     sub